package com.archerdu.demo.lucene;

import java.io.File;
import java.io.IOException;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.queryparser.classic.ParseException;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.FSDirectory;
import org.junit.Test;

public class SearchDemo {

    @Test
    public void testSearch() {
        SearchDemo sd = new SearchDemo();
        System.out.println("\n---china---");
        sd.search("china");
        System.out.println("\n---hebei---");
        sd.search("hebei");
        System.out.println("\n---shijiazhuang---");
        sd.search("shijiazhuang");
        System.out.println("\n---beijing---");
        sd.search("beijing");
    }

    public void search(String queries) {
        String field = Constant.FIELD_CONTENTS;
        int hitsPerPage = 10;
        int page = 1;
        IndexReader reader;

        try {
            // 建立搜索引擎
            reader = DirectoryReader.open(FSDirectory.open(new File(Constant.INDEX_PATH)));
            IndexSearcher searcher = new IndexSearcher(reader);
            Analyzer analyzer = new StandardAnalyzer(Constant.CURRENT_VERSION);
            // 创建查询
            Query query = null;
            QueryParser parser = new QueryParser(Constant.CURRENT_VERSION, field, analyzer);
            query = parser.parse(queries);

            // 执行搜索
            doPagingSearch(searcher, query, hitsPerPage, page);

        } catch (IOException e) {
            e.printStackTrace();
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }

    private void doPagingSearch(IndexSearcher searcher, Query query, int hitsPerPage, int page) throws IOException {
        TopDocs results = searcher.search(query, 5 * hitsPerPage);
        ScoreDoc[] hits = results.scoreDocs;
        int numTotalHits = results.totalHits;
        System.out.println(numTotalHits + " total matching documents \n");

        // 可以实现分页
        int start = 0;
        int end = Math.min(numTotalHits, hitsPerPage);
        if (end > hits.length) {
            System.out.println("Only results 1 - " + hits.length + " of " + numTotalHits
                    + " total matching documents collected.");
            System.out.println("Collect more (y/n) ?");
            hits = searcher.search(query, numTotalHits).scoreDocs;
        }

        end = Math.min(hits.length, start + hitsPerPage);

        for (int i = start; i < end; ++i) {
            Document doc = searcher.doc(hits[i].doc);
            String path = doc.get(Constant.FIELD_ID);
            if (path != null) {
                System.out.println(i + 1);
                System.out.println("doc.score : " + hits[i].score);
                System.out.println("path : " + path);
                System.out.println(Constant.FIELD_MODIFIED + " : " + doc.get(Constant.FIELD_MODIFIED));
            } else {
                System.out.println((i + 1) + ". " + "No path for this document");
            }
        }
        if (end == 0)
            return;
    }
}
